package com.hz.friends.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

import com.hz.friends.customView.CustomToast;
import com.hz.friends.customView.CustomWaitDialog;

public abstract class BaseActivity extends Activity {

	protected List<AsyncTask<Void, Void, Boolean>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, Boolean>>();
    protected final String TAG = this.getClass().getSimpleName();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	/** ��ʼ������ **/
	protected abstract void init();

	/** ��ʼ����ͼ **/
	protected abstract void initViews();

	/** ���ÿؼ����� **/
	protected abstract void setViews();

	/** ��ʼ���¼� **/
	protected abstract void initEvents();

	protected void putAsyncTask(AsyncTask<Void, Void, Boolean> asyncTask) {
		mAsyncTasks.add(asyncTask.execute());
	}

	protected void clearAsyncTask() {
		Iterator<AsyncTask<Void, Void, Boolean>> iterator = mAsyncTasks
				.iterator();
		while (iterator.hasNext()) {
			AsyncTask<Void, Void, Boolean> asyncTask = iterator.next();
			if (asyncTask != null && !asyncTask.isCancelled()) {
				asyncTask.cancel(true);
			}
		}
		mAsyncTasks.clear();
	}

	public void showToast(String text) {
		// Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
		CustomToast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
    public void showSysToast(String text){
		 Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
	/** ͨ��Class��ת���� **/
	protected void startActivity(Class<?> cls) {
		startActivity(cls, null);
	}

	/** ����Bundleͨ��Class��ת���� **/
	protected void startActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent();
		intent.setClass(this, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

	/** ͨ��Action��ת���� **/
	protected void startActivity(String action) {
		startActivity(action, null);
	}

	/** ����Bundleͨ��Action��ת���� **/
	protected void startActivity(String action, Bundle bundle) {
		Intent intent = new Intent();
		intent.setAction(action);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

	private Dialog progressDialog;

	public void createWiatDialog(String str) {
		cancleWaitDialog();
		progressDialog = new CustomWaitDialog(this,R.style.CustomWaitDialog);
		progressDialog.setTitle(str);
		progressDialog.show();
	}

	public void CreatAboutDialog(final String str,final String title) {
		// new AlertDialog.Builder(this, R.style.MyDialog).setMessage(str)
		new AlertDialog.Builder(this).setTitle(title).setMessage(str)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).create().show();

	}

	public void cancleWaitDialog() {
		if (progressDialog != null && progressDialog.isShowing())
			progressDialog.cancel();
	}

	@Override
	protected void onDestroy() {
		clearAsyncTask();
		super.onDestroy();
	}
	
	
	
}
